package com.daidai.activitys

import android.os.Handler
import android.os.Looper
import android.widget.ArrayAdapter
import android.widget.ImageSwitcher
import android.widget.ImageView
import android.widget.Spinner
import com.daidai.base.BaseActivity
import com.daidai.custom.MyTextView
import com.daidai.spinner.R
import java.util.*

/**
 * Spinner
 * ImageSwitcher 基本使用
 */
class SpinnerActivity : BaseActivity() {

    lateinit var spinner:Spinner
    lateinit var adapter:ArrayAdapter<String>
    lateinit var switcher:ImageSwitcher

    lateinit var myTextView: MyTextView

    companion object{
        val imgLst = arrayListOf<Int>(
            R.mipmap.img_1,
            R.mipmap.img_2,
            R.mipmap.img_3,
            R.mipmap.img_4,
            R.mipmap.img_5,
            R.mipmap.img_6,
        )
    }

    var idx = 0
    val handler = Handler(Looper.getMainLooper())
    var timer:Timer? = null

    override fun getContentViewResId() = R.layout.activity_spinner

    override fun initView() {
        spinner = findViewById(R.id.spinner)
        myTextView = findViewById(R.id.myTextView)
        val dataSource = arrayListOf<String>("zhangsan","lisi","wangwu","zhaoliu")
        adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,dataSource)
        spinner.adapter = adapter
        switcher = findViewById(R.id.switcher)
        switcher.setFactory {
            val imageView = ImageView(this)
            imageView.scaleType = ImageView.ScaleType.FIT_CENTER
            imageView
        }
        switcher.setImageResource(imgLst[idx])
    }

    override fun initData() {
        timer = Timer()
        timer?.schedule(task,1000,3000)
    }

    var task = object : TimerTask(){
        override fun run() {
            handler.post {
                switcher.setImageResource(imgLst[idx])
                idx = (idx + 1) % imgLst.size
            }
        }
    }

    override fun onDestroy() {
        timer?.cancel()
        timer = null
        super.onDestroy()
    }
}